package com.trailwalker.javafx.core.activity;

import com.trailwalker.javafx.core.application.JavaFxApplication;
import com.trailwalker.javafx.core.entity.Intent;
import com.trailwalker.javafx.core.fragment.FragmentManager;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Trailwalker
 * @create 2021-09-05 15:38
 */
public class Activity extends ActivityContext {

    private JavaFxApplication mApplication;
    private Pane mContainerView;
    private Pane mContentView;
    private List<FragmentManager> mFragmentManagerList;
    private double xOffset = 0;
    private double yOffset = 0;
    private Intent mIntent;

    @Override
    public void onCreate() {
        mFragmentManagerList = new ArrayList<>();
    }

    @Override
    public void startActivity(Intent intent) {
        mApplication.startActivity(intent);
    }

    @Override
    public void finish() {
        mApplication.finish(this);
    }

    @Override
    public void setIntent(Intent intent) {
        mIntent = intent;
    }

    public Intent getIntent() {
        return mIntent;
    }

    @Override
    public void setContainerView(Pane containerView) {
        mContainerView = containerView;
    }

    @Override
    public void setApplication(JavaFxApplication application) {
        mApplication = application;
    }

    @Override
    public void onRestart() {
        setContentView(mContentView);
    }

    @Override
    public void onDestroy() {

    }

    public void addFragmentManager(FragmentManager fragmentManager) {
        fragmentManager.setActivity(this);
        mFragmentManagerList.add(fragmentManager);
        if (fragmentManager.getMainFragment() != null) {
            fragmentManager.startFragment(fragmentManager.getMainFragment());
        }
    }

    public JavaFxApplication getApplication() {
        return mApplication;
    }

    public void setContentView(Pane contentView) {
        if (!mContainerView.getChildren().contains(contentView)) {
            mContentView = contentView;
            mContainerView.getChildren().clear();
            mContainerView.getChildren().add(mContentView);
            AnchorPane.setTopAnchor(mContentView, 0.0);
            AnchorPane.setRightAnchor(mContentView, 0.0);
            AnchorPane.setBottomAnchor(mContentView, 0.0);
            AnchorPane.setLeftAnchor(mContentView, 0.0);
        }
    }

    protected void setTitleView(Stage dialogStage, Pane dragAblePane) {
        if (dragAblePane != null) {
            dragAblePane.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    xOffset = event.getSceneX();
                    yOffset = event.getSceneY();
                }
            });

            dragAblePane.setOnMouseDragged(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    dialogStage.setX(event.getScreenX() - xOffset);
                    dialogStage.setY(event.getScreenY() - yOffset);
                }
            });
        }
    }

    @Override
    public boolean onBackPressed() {
        boolean isHandled = false;
        for (int i = 0; i < mFragmentManagerList.size(); i++) {
            FragmentManager fragmentManager = mFragmentManagerList.get(i);
            if (fragmentManager.onBackPressed()) {
                isHandled = true;
            }
        }
        return isHandled;
    }
}
